<?php

/**
 * Framework_DB_DaoNodeFactory
 * *
 * @author Application Support Group
 * @copyright University of Georgia
 * @package BAMF
 * @filesource
*/


abstract class Framework_DB_DaoNodeFactory {

	public static function build( $type, $parameters=null, $pager=null ) {

		$_output = false ;

		if( !$parameters instanceof Framework_DB_DaoParameters ) {
			$parameters = new Framework_DB_DaoParameters() ;
		}
		if( !$pager instanceof Framework_DB_DaoCollectionPager ) {
			$pager = new Framework_DB_DaoCollectionPager() ;
		}

		if (strpos($type, APPLICATION_DAO_NODE_PREFIX) === 0) {
			$class = $type ;
		} else {
			$class = APPLICATION_DAO_NODE_PREFIX . $type ;			
		}

		$daoType = eval( "return $class::DAO_TYPE ;" ) ;
		$collection = null ;

		switch( $daoType ) {
			case "DaoNode":
				$collection = new Framework_DB_DaoCollection( $class, $parameters, $pager ) ;
				break ;

			case "DaoRelation":
				$collection = new Framework_DB_RelationCollection( $class, $parameters, $pager ) ;
				break ;
		}


		if( $collection instanceof Framework_DB_DaoCollection ) {
			if( Framework_ErrorHandler::isError( $collection->_data, __METHOD__."::".__LINE__ )) {
				$_output = $collection->_data ;
			} else {
				if( $collection->_size > 1 ) {
					$_output = Framework_ErrorHandler::raiseError( 
						"Multiple objects with requested property found.", 
						__METHOD__."::".__LINE__  
						) ;
					$_output->addUserInfo("multiple") ;
					
				} elseif( $collection->_size == 0 ) {
					$_output = Framework_ErrorHandler::raiseError( 
						"No object with requested property found.", 
						__METHOD__."::".__LINE__  
						) ;
					$_output->addUserInfo("none") ;
					
				} else {
					$_output = $collection->_data[0] ;
				}
			}
		} else {
			$_output = Framework_ErrorHandler::raiseError( 
				"Invalid or non-existent DaoCollection class: $type", 
				__METHOD__."::".__LINE__ 
				) ;
		}

		return $_output ;

	}// end build
}

?>
